const setParams = params => {
  let newArray = []
  for (let key in params) { 
    let str = key + '=' + params[key]
    newArray.push(str)
  }
  return newArray.join('&')
}

function fangHaoAjax(options) {
  const p = new Promise((resolve, reject) => { 
    const xhr = new XMLHttpRequest()
    xhr.responseType = 'json'
    const query = setParams(options.data)
    if (options.method.toUpperCase() === 'GET') {
      xhr.open('GET', query ? options.url + '?' + query : options.url)
      xhr.send()
    } else if (options.method.toUpperCase() === 'POST') {
      xhr.open('POST', options.url)
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      xhr.send(query)
    }
    // 监听
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
        if (xhr.status === 200) {
          resolve(xhr.response)
        } else {
          reject(xhr.response)
        }
      }
    }
  })
    return p 
}